package com.neusoft.utils.email;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

import com.neusoft.utils.config.SystemConfig;
import com.neusoft.utils.convert.ConvertUtils;
import com.neusoft.utils.email.EmailUtil.EncryptionTypes;
import com.neusoft.utils.email.bean.EmailBean;
import com.neusoft.utils.email.bean.Receiver;
import com.neusoft.utils.email.callback.EmailOneSendResult;
import com.neusoft.utils.map.MyHashMap;
import com.neusoft.utils.template.TplUtil;

public class TEmailUtil {
	   private  String HOST;
	   private  Integer PORT;
	   private  String ACCOUNT;
	   private  String ACCOUNTNAME;
	   private  String PASSWORD;
	   private EmailUtil email ;
	   public TEmailUtil() {
		    this.HOST=SystemConfig.getProperty("enroll.email.host");
		    this.PORT=ConvertUtils.toInteger(SystemConfig.getProperty("enroll.email.port"));
		    this.ACCOUNT=SystemConfig.getProperty("enroll.email.account");
		    this.ACCOUNTNAME=SystemConfig.getProperty("enroll.email.account.name");
		    this.PASSWORD=SystemConfig.getProperty("enroll.email.password");
		    this.email = new EmailUtil(HOST, 
        			PORT, EncryptionTypes.Default.ordinal(), true, ACCOUNT, PASSWORD, true);
	   }
       private  int   sendHtmlToOne(MyHashMap map,String address,String title,String html,Map<String,String> param,
        		EmailOneSendResult result){
    	    if(null!=param&&param.size()>0){
    	    	html=TplUtil.replaceAllByMap(html, param);
    	    }
        	try {
				email.sendEmail(ACCOUNT, ACCOUNTNAME, address, title, html);
				if(null!=result){
					result.success(map,address);
				}
				return 1;
			} catch (Exception e) {
				if(null!=result){
					result.error(map,address);
				}
				e.printStackTrace();
				return 0;
			}
        }
        public int sendHtmlToList(EmailBean emailBean){
        	int count=0;
        	for (Receiver receiver : emailBean.getReceiverList()) {
        	 int resultCount =	sendHtmlToOne(receiver.getMap(),receiver.getAddress(), emailBean.getTitle(), emailBean.getContent(),
        				receiver.getParam(),emailBean.getEmailOneSendResult());
        	 count+=resultCount;
			}
        	if(null!=emailBean.getEmailSendResult()){
            	emailBean.getEmailSendResult().result(emailBean.getMap(), count);
        	}
        	return count;
        }
        public void sendEmail(EmailBean emailBean){
        	Executors.newSingleThreadExecutor().execute(new EmailSendTask(emailBean));
        }
        public void sendOneEmail(MyHashMap map,String address,String title,String content,Map<String,String> param,
        		EmailOneSendResult result){
        	EmailBean emailBean = new EmailBean();
        	emailBean.setTitle(title);
        	emailBean.setContent(content);
        	List<Receiver> receiverList = new ArrayList<Receiver>();
        	receiverList.add(new Receiver().setMap(map).setAddress(address).setParam(param));
        	emailBean.setReceiverList(receiverList);
        	emailBean.setEmailOneSendResult(result);
        	Executors.newSingleThreadExecutor().execute(new EmailSendTask(emailBean));
        }
}
